package schoolWork.work06.test2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class mainWindow extends JFrame implements ActionListener {
    //定义组件(由于组件比较多,所以在具体使用的时候,再实例化)
    JFrame mainjframe;
    Container cont;
    JSplitPane jsp;
    JPanel Left_parentpanel, Right_parentpanel;
    JPanel Right_toppanel;
    JPanel Left_botpanel, Right_botpanel;
    JMenuBar menuBar;
    JMenu menu_file, menu_edit;
    JMenuItem item_new, item_open, item_cut, item_copy;
    JLabel lbl_username, lbl_sex, lbl_department, lbl_hobby, lbl_phone, lbl_resume;
    JTextField txt_username, txt_phone;
    JRadioButton rdo_male, rdo_female;
    JComboBox cmb_department;
    JCheckBox ckb_music, ckb_reading, ckb_cooking;
    JTextArea txt_resume, txt_showInfo;

    JButton btn_confirm, btn_cancel, btn_save;

    public mainWindow() {
        mainjframe = new JFrame();
        cont = mainjframe.getContentPane();

        menuBar = new JMenuBar();
        menu_file = new JMenu("File");
        menu_edit = new JMenu("Edit");

        item_new = new JMenuItem("New");
        item_new.addActionListener(this);
        item_new.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
        item_open = new JMenuItem("Open");
        item_open.addActionListener(this);

        item_cut = new JMenuItem("Cut");
        item_cut.addActionListener(this);
        item_copy = new JMenuItem("Copy");
        item_copy.addActionListener(this);

        menu_file.add(item_new);
        menu_file.add(item_open);
        menu_edit.add(item_cut);
        menu_edit.add(item_copy);
        menuBar.add(menu_file);
        menuBar.add(menu_edit);
        mainjframe.setJMenuBar(menuBar);


        Left_parentpanel = new JPanel(new BorderLayout());
        Right_parentpanel = new JPanel(new BorderLayout());
        jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, Left_parentpanel, Right_parentpanel);
        jsp.setDividerSize(10);
        jsp.setDividerLocation(300);

        Box Left_toppanel = Box.createVerticalBox();
        //Left_toppanel = new JPanel(new BoxLayout(Left_parentpanel,BoxLayout.X_AXIS));
        Left_toppanel.setBorder(BorderFactory.createTitledBorder("Please input " +
                "your information"));
        Right_toppanel = new JPanel();
        Right_toppanel.setBorder(BorderFactory.createTitledBorder("Your information:"));
        Left_botpanel = new JPanel();
        Right_botpanel = new JPanel();

        Box box1 = new Box(BoxLayout.X_AXIS);
        box1.add(lbl_username = new JLabel("username         "));
        box1.add(txt_username = new JTextField(10));
        Left_toppanel.add(box1);

        Box box2 = new Box(BoxLayout.X_AXIS);
        rdo_male = new JRadioButton("male", true);
        rdo_female = new JRadioButton("female", false);
        ButtonGroup group = new ButtonGroup();
        group.add(rdo_male);
        group.add(rdo_female);
        box2.add(lbl_sex = new JLabel("sex                                 "));
        box2.add(rdo_male);
        box2.add(rdo_female);
        Left_toppanel.add(box2);

        Box box3 = new Box(BoxLayout.X_AXIS);
        box3.add(lbl_department = new JLabel("department    "));
        box3.add(cmb_department = new JComboBox(new Object[]{"Math", "English"
                , "computer"}));
        Left_toppanel.add(box3);

        Box box4 = new Box(BoxLayout.X_AXIS);
        ckb_music = new JCheckBox("music", true);
        ckb_reading = new JCheckBox("reading", false);
        ckb_cooking = new JCheckBox("cooking", false);
        box4.add(lbl_hobby = new JLabel("hobby                 "));
        box4.add(ckb_music);
        box4.add(ckb_reading);
        box4.add(ckb_cooking);
        Left_toppanel.add(box4);

        Box box5 = new Box(BoxLayout.X_AXIS);
        box5.add(lbl_phone = new JLabel("telephoneNo    "));
        box5.add(txt_phone = new JTextField(10));
        Left_toppanel.add(box5);

        Box box6 = new Box(BoxLayout.X_AXIS);
        box6.add(lbl_resume = new JLabel("resume            "));
        txt_resume = new JTextArea(10, 10);
        JScrollPane js = new JScrollPane(txt_resume);
        box6.add(js);
        Left_toppanel.add(box6);

        btn_confirm = new JButton("Confirm");
        btn_confirm.addActionListener(this);
        btn_cancel = new JButton("Cancel");
        btn_cancel.addActionListener(this);
        Left_botpanel.add(btn_confirm);
        Left_botpanel.add(btn_cancel);

        Left_parentpanel.add(Left_toppanel, BorderLayout.CENTER);
        Left_parentpanel.add(Left_botpanel, BorderLayout.SOUTH);

        txt_showInfo = new JTextArea(50, 50);
        JScrollPane js_show = new JScrollPane(txt_showInfo);
        Right_toppanel.add(js_show);
        btn_save = new JButton("Save");
        btn_save.addActionListener(this);
        Right_botpanel.add(btn_save);
        Right_parentpanel.add(Right_toppanel, BorderLayout.CENTER);
        Right_parentpanel.add(Right_botpanel, BorderLayout.SOUTH);

        cont.add(jsp);
        mainjframe.setSize(800, 400);
        mainjframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainjframe.setLocationRelativeTo(null);
        mainjframe.setVisible(true);


    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == item_new) {
           clearAll();
        }
        if (e.getSource() == item_open) {
            System.out.println(txt_username.getText()+ "点击了Open菜单项");
        }
        if (e.getSource() == item_cut) {
            System.out.println(txt_username.getText() + "点击了Cut菜单项");
        }
        if (e.getSource() == item_copy) {
            System.out.println(txt_username.getText() + "点击了Copy菜单项");
        }
        if(e.getSource() == btn_confirm){
            String sex;
            if(rdo_male.isSelected())
                sex = "male";
            else sex = "female";
            StringBuffer hobby = new StringBuffer();
            if(ckb_music.isSelected())
                hobby.append("music ");
            if(ckb_reading.isSelected())
                hobby.append("reading ");
            if(ckb_cooking.isSelected())
                hobby.append("cooking ");
            //将输入的信息输入到txt_showInfo中
            txt_showInfo.setText("my name: " +"\t\t"+ txt_username.getText() + "\n"
                    + "sex:" + "\t\t" + sex + "\n"
                    + "department:" + "\t\t" +  cmb_department.getSelectedItem() + "\n"
                    + "hobby:" + "\t\t" + hobby.toString() + "\n"
                    + "telephone No."+ "\t" + txt_phone.getText() + "\n"
                    + "resume:" + "\n"
                    + txt_resume.getText()
            );
        }
        if(e.getSource() == btn_cancel){
            int n = JOptionPane.showConfirmDialog(this,"Do you confirm to delete it?"
            ,"Warning",JOptionPane.YES_NO_OPTION);
            if(n == JOptionPane.YES_OPTION){
                clearAll();
            }
        }
        if(e.getSource() == btn_save){
            System.out.println(txt_username.getText() + "信息已保存");
        }


    }

    private void clearAll(){
        //将各个输入部分清空
        txt_username.setText("");
        txt_phone.setText("");
        txt_resume.setText("");
        txt_showInfo.setText("");
        rdo_male.setSelected(true);
        ckb_music.setSelected(true);
        ckb_reading.setSelected(false);
        ckb_cooking.setSelected(false);
    }
}
